package com.secxiot.consumer.user.controller;

import com.secxiot.common.entity.ResultVo;
import com.secxiot.consumer.user.feign.UserClient;
import com.secxiot.provider.communication.dto.CommunicationCreateDto;
import com.secxiot.provider.communication.dto.CommunicationDetailCreateDto;
import com.secxiot.provider.communication.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "医患模块")
@RestController
@RequestMapping("communications")
public class CommunicationController {

    @Autowired
    private UserClient userClient;

    @ApiOperation(value = "医生查看：病患管理-患者列表", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "登录用户id", required = true, paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "userType", value = "登录用户类型，字典00001", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "patientName", value = "搜索：患者名称", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "pageNo", value = "页码", required = true, paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "pageSize", value = "条数", required = true, paramType = "query", dataType = "Long")})
    @GetMapping("/patientList")
    public ResultVo<List<CommunicatePatientVo>> getPatientList(
            @RequestParam(value = "userId", required = true) Long userId,
            @RequestParam(value = "userType", required = true) String userType,
            @RequestParam(value = "patientName", required = false) String patientName,
            @RequestParam(value = "pageNo", required = true) Long pageNo,
            @RequestParam(value = "pageSize", required = true) Long pageSize) {

        return new ResultVo<>(userClient.getPatientList(userId, userType, patientName, pageNo, pageSize));

    }

    @ApiOperation(value = "患者查看：我的医生列表", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "登录用戶id", required = true, paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "doctorName", value = "医生名称", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "pageNo", value = "页码", required = true, paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "pageSize", value = "条数", required = true, paramType = "query", dataType = "Long")})
    @GetMapping("/doctorList")
    public ResultVo<List<CommunicateDoctorVo>> getDoctorList(
            @RequestParam(value = "userId", required = true) Long userId,
            @RequestParam(value = "doctorName", required = false) String doctorName,
            @RequestParam(value = "pageNo", required = true) Long pageNo,
            @RequestParam(value = "pageSize", required = true) Long pageSize) {
        return new ResultVo<List<CommunicateDoctorVo>>(userClient.getDoctorList(userId, doctorName, pageNo, pageSize));
    }

    @ApiOperation(value = "修改患者信息（备注、床号）", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "patientId", value = "患者id", required = true, paramType = "form", dataType = "Long"),
            @ApiImplicitParam(name = "memo", value = "备注", required = false, paramType = "form", dataType = "String"),
            @ApiImplicitParam(name = "beds", value = "床号", required = false, paramType = "form", dataType = "String")})
    @PostMapping("/updatePatientExtro")
    public ResultVo<Boolean> updatePatientExtro(@RequestParam(value = "patientId", required = true) Long patientId,
                                                @RequestParam(value = "memo", required = false) String memo,
                                                @RequestParam(value = "beds", required = false) String beds) {
        return new ResultVo<>(userClient.updatePatientExtro(patientId, memo, beds));
    }

    @ApiOperation(value = "修改医生信息（备注）", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "doctorId", value = "医生id", required = true, paramType = "form", dataType = "Long"),
            @ApiImplicitParam(name = "memo", value = "备注", required = true, paramType = "form", dataType = "String")})
    @PostMapping("/updateDoctorExtro")
    public ResultVo<Boolean> updateDoctorExtro(@RequestParam(value = "doctorId", required = true) Long doctorId,
                                               @RequestParam(value = "memo", required = false) String memo) {
        return new ResultVo<>(userClient.updateDoctorExtro(doctorId, memo));
    }

    @ApiOperation(value = "获取主题列表", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "登录用户id", required = true, paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "userType", value = "登录用户类型，字典00001", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "doctorId", value = "医生/护士id", required = true, paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "patientId", value = "患者id", required = true, paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "title", value = "搜索：主题标题", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "pageNo", value = "页码", required = true, paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "pageSize", value = "条数", required = true, paramType = "query", dataType = "Long")})
    @GetMapping("/communicationlist")
    public ResultVo<List<CommunicationVo>> getCommunicationlist(
            @RequestParam(value = "userId", required = true) Long userId,
            @RequestParam(value = "userType", required = true) String userType,
            @RequestParam(value = "doctorId", required = true) Long doctorId,
            @RequestParam(value = "patientId", required = true) Long patientId,
            @RequestParam(value = "title", required = false) String title,
            @RequestParam(value = "pageNo", required = true) Long pageNo,
            @RequestParam(value = "pageSize", required = true) Long pageSize) {

        return new ResultVo<List<CommunicationVo>>(
                userClient.getCommunicationlist(userId, userType, doctorId, patientId, title, pageNo, pageSize));
    }

    @ApiOperation(value = "获取主题对话列表", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "readUserId", value = "读取人id", required = true, paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "communicationId", value = "主题id", required = true, paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "pageNo", value = "页码", required = true, paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "pageSize", value = "条数", required = true, paramType = "query", dataType = "Long")})
    @GetMapping("/communicationDetaillist")
    public ResultVo<List<CommunicationDetailVo>> getCommunicationDetaillist(
            @RequestParam(value = "readUserId", required = true) Long readUserId,
            @RequestParam(value = "communicationId", required = true) Long communicationId,
            @RequestParam(value = "pageNo", required = true) Long pageNo,
            @RequestParam(value = "pageSize", required = true) Long pageSize) {
        return new ResultVo<List<CommunicationDetailVo>>(
                userClient.getCommunicationDetaillist(readUserId, communicationId, pageNo, pageSize));
    }

    @ApiOperation(value = "添加新主题", httpMethod = "POST")
    @PostMapping("/createCommunication")
    public ResultVo<Long> createCommunication(@RequestBody CommunicationCreateDto communicationCreateDto) {
        return new ResultVo<Long>(userClient.createCommunication(communicationCreateDto));
    }

    @ApiOperation(value = "添加新主题对话", httpMethod = "POST")
    @PostMapping("/createCommunicationDetail")
    public ResultVo<Long> createCommunicationDetail(
            @RequestBody CommunicationDetailCreateDto communicationCreateDetailDto) {
        return new ResultVo<Long>(userClient.createCommunicationDetail(communicationCreateDetailDto));
    }

    @ApiOperation(value = "删除主题", httpMethod = "DELETE")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "删除人的用户id", required = true, paramType = "form", dataType = "Long"),
            @ApiImplicitParam(name = "communicationId", value = "主题id", required = true, paramType = "form", dataType = "Long")})
    @DeleteMapping("/deleteCommunication")
    public ResultVo<Boolean> deleteCommunication(@RequestParam(value = "userId", required = true) Long userId,
                                                 @RequestParam(value = "communicationId", required = true) Long communicationId) {
        return new ResultVo<Boolean>(userClient.deleteCommunication(userId, communicationId));
    }

    @ApiOperation(value = "修改主题状态为已读", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "communicationId", value = "主题id", required = true, paramType = "form", dataType = "Long"),
            @ApiImplicitParam(name = "communicationDetailId", value = "最新主题对话id", required = false, paramType = "form", dataType = "Long"),
            @ApiImplicitParam(name = "readUserId", value = "读取人的用户id", required = true, paramType = "form", dataType = "Long")})
    @PostMapping("/updateCommunicationReadFlag")
    public ResultVo<Boolean> updateCommunicationReadFlag(
            @RequestParam(value = "communicationId", required = true) Long communicationId,
            @RequestParam(value = "communicationDetailId", required = false) Long communicationDetailId,
            @RequestParam(value = "readUserId", required = true) Long readUserId) {
        return new ResultVo<Boolean>(
                userClient.updateCommunicationReadFlag(communicationId, communicationDetailId, readUserId));
    }

    @ApiOperation(value = "获取未读数量", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "readUserId", value = "读取人id", required = true, paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "userType", value = "登录用户类型，字典00001", required = true, paramType = "query", dataType = "String")})
    @GetMapping("/unread/count")
    public ResultVo<CommunicationUnreadCount> getUnreadCount(
            @RequestParam(value = "readUserId", required = true) Long readUserId,
            @RequestParam(value = "userType", required = true) String userType) {
        return new ResultVo<CommunicationUnreadCount>(userClient.getUnreadCount(readUserId, userType));
    }

    /**
     * ==================================================================================================================================================
     */
    @ApiOperation(value = "医患-获取对话列表", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "talkUserId", value = "对方userId", required = true, paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "pageNo", value = "页码", required = true, paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "pageSize", value = "条数", required = true, paramType = "query", dataType = "Long")})
    @GetMapping("/getcommunicationTalklist")
    public ResultVo<List<CommunicationTalkVo>> getcommunicationTalklist(
            @RequestParam(value = "talkUserId", required = true) Long talkUserId,
            @RequestParam(value = "userId", required = true) Long userId,
            @RequestParam(value = "pageNo", required = true) Long pageNo,
            @RequestParam(value = "pageSize", required = true) Long pageSize) {
        return new ResultVo<List<CommunicationTalkVo>>(
                userClient.getcommunicationTalklist(talkUserId, userId, pageNo, pageSize));
    }

    @ApiOperation(value = "医患-发送消息", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "talkUserId", value = "对方userId", required = true, paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "content", value = "内容", required = true, paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "contentType", value = "内容类型1:文本,2图片,3文件", required = true, paramType = "query", dataType = "Integer")})
    @PostMapping("/sendTalkMessage")
    public ResultVo<Long> sendTalkMessage(
            @RequestParam(value = "talkUserId", required = true) Long talkUserId,
            @RequestParam(value = "userId", required = true) Long userId,
            @RequestParam(value = "content", required = true) String content,
            @RequestParam(value = "contentType", required = true) Integer contentType) {
        return new ResultVo<Long>(
                userClient.sendTalkMessage(talkUserId, userId, content, contentType));
    }
}
